# -*- coding: utf-8 -*-
#===================================================
# @Time : 2021/5/17 23:20                           
# @Author : weni09                                  
# @File : Depends.py
#===================================================


from fastapi import Depends
from admin.fastapi_admin.depends import jwt_optional
from admin.fastapi_admin.factory import app
from datetime import datetime
import random

def gen_rnd_filename():
    """
    :return: 随机文件名前缀--年月日时分秒+4位随机数
    """
    filename_prefix = datetime.now().strftime('%Y%m%d%H%M%S')
    return '%s%s' % (filename_prefix, str(random.randrange(1000, 10000)))

async def get_user(user_id=Depends(jwt_optional)):
    if user_id is None:
        return
    user = await app.user_model.get_or_none(pk=user_id)
    if not user:
        return
    return user